<template>
  <div class="content-warpper">
    <div class="left-tree">
      <el-input
        placeholder="输入主题名称"
        v-model="filterText"
        class="form-input"
        suffix-icon="el-icon-search"
        size="small"
      >
      </el-input>
      <el-tree
        v-if="isShowTree"
        class="filter-tree"
        :data="subjectList"
        :props="defaultProps"
        @node-click="handleNodeClick"
        :default-expand-all="true"
        :filter-node-method="filterNode"
        node-key="id"
        :default-checked-keys="[selectedNodeID]"
        :highlight-current="true"
        ref="tree"
      ></el-tree>
    </div>
    <div class="server-main right-content">
      <el-form :inline="true" v-model="modelData" class="search-form">
        <el-form-item label="模型名称" class="form-input">
          <el-input
            placeholder="请输入模型名称"
            v-model.trim="modelData.modelName"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="数据源" class="form-input">
          <el-select v-model="modelData.dataSourceId">
            <el-option
              v-for="item in dataSourceList"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" class="status form-input">
          <el-select v-model="modelData.modelStatus">
            <el-option label="全部" value=""></el-option>
            <el-option label="编辑中" :value="0"></el-option>
            <el-option label="已发布" :value="1"></el-option>
            <el-option label="已停用" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button id="btn" size="small" type="primary" @click="onSubmit(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>

      <div class="server-table">
        <div class="table-title">
          <span class="table-title-text">数据维护</span>
        </div>
        <el-table :data="modelList" class="km-table">
          <el-table-column
            type="index"
            label="ID"
            width="50"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="modelName"
            label="模型名称"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="modelCode"
            label="模型编码"
            align="center"
            show-overflow-tooltip
          ></el-table-column>

          <el-table-column
            prop="tableName"
            label="表名"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="themeName"
            label="所属主题"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="dataSourceName"
            label="数据源"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="dataCount"
            label="数据量"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column label="操作" align="center" width="100">
            <template slot-scope="scope">
              <el-link
                :underline="false"
                type="primary"
                class="ontology-edit"
                @click.native.prevent="detail(scope.row)"
                >查看</el-link
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="km-pagination"
          background
          :small="true"
          layout="total, prev, pager, next,sizes, jumper"
          :total="total"
          :hide-on-single-page="!total"
          :page-size.sync="modelData.pageSize"
          :pager-count="7"
          :page-sizes="[10, 20, 30]"
          :current-page="modelData.pageNum"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "DataMaintenance",
  components: {},
  data: () => {
    return {
      modelId: "", // 属性 id
      isShowTree: false, //是否显示树
      selectedNodeID: -1, //树选中节点id
      selectedID: -1,
      selectedNode: null,
      modelData: {
        dataSourceId: "", // 数据源Id
        modelName: "", //查询--模型名称
        modelStatus: "", //查询--状态
        pageNum: 1, // 页码
        pageSize: 10, // 每页条数
      },
      dataSourceList: [], //数据源列表
      modelList: [], // 逻辑模型列表
      subjectList: [], //主题树列表
      filterText: "", //树查询关键字
      defaultProps: {
        //树-展示属性设置
        children: "children",
        label: "label",
      },
      loading: false,
      total: 0, // 数据总条数
    };
  },
  created() {
    this.getSubjectList();
    this.getAllDataSource();
  },
  watch: {
    filterText(val) {
      //树结构查询关键字
      this.$refs.tree.filter(val);
    },
  },
  methods: {
    //树结构，筛选方法
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    //点击树节点-回调
    handleNodeClick(data) {
      this.selectedNodeID = data.id;
      this.selectedNode = data;
      this.selectedId = data.id;
      if (!data.children || data.children.length === 0) {
        this.getModelList(data.id);
      }
    },
    // 查询
    onSubmit(pageNum) {
      this.modelData.pageNum = pageNum ? pageNum : this.modelData.pageNum;
      this.getModelList(this.selectedNodeID);
    },
    // 展示树
    showTree(ID) {
      this.isShowTree = true;
      this.selectedNodeID = ID;
      this.getModelList(ID);
      this.selectedID != -1
        ? this.handleNodeClick(this.selectedNode)
        : this.handleNodeClick(this.subjectList[0]);
      this.$nextTick(() => {
        this.$refs.tree.setCurrentKey(this.selectedNodeID); //一定要加这个选中了否则样式没有出来
      });
    },
    // 数据源列表
    getAllDataSource() {
      let params = {
        code: "target_base",
        source: 3,
      };
      this.$axios
        .post(this.$api.dataCollection.getAllSourceList, params)
        .then((res) => {
          let { code, message, data } = res.data;
          if (code !== 200) {
            this.$message.warning(message);
            return;
          } else {
            this.dataSourceList = data;
          }
        });
    },
    // 获取主题列表
    getSubjectList() {
      this.$axios
        .post(this.$api.dataCollection.getSubjectList)
        .then((res) => {
          let data = res.data.data;
          this.subjectList = data;
          if (data.length > 0) {
            this.selectedID != -1
              ? this.showTree(this.selectedID)
              : this.showTree(data[0].id);
          }
        })
        .catch((err) => {
          console.error(err);
        });
    },
    // 获取模型列表
    getModelList(ID) {
      let themeId = { themeId: ID };
      let params = { ...this.modelData, ...themeId };
      this.$axios
        .post(this.$api.dataCollection.getModelList, params)
        .then((res) => {
          let data = res.data.data.list;
          this.modelList = data;
          this.total = res.data.data.totalCount;
        })
        .catch((err) => {
          console.error(err);
        });
    },
    // 修改当前页码
    handleCurrentChange(page) {
      this.modelData.pageNum = page;
      this.getTableList();
    },
    //修改当前页面容量
    handleSizeChange(page) {
      this.modelData.pageSize = page;
      this.getTableList();
    },
    detail(row) {
      let id = row.modelId;
      this.$router.push(
        `/dataReceive/dataCollection/dataMaintenance/detail/${id}`
      );
    },
  },
};
</script>
<style>
.dialog-warpper {
  padding: 20px;
}
</style>
<style lang="scss" scoped>
.content-warpper {
  display: flex;
  width: 100%;
  height: 92%;
  background: #fff;
  border-radius: 4px;
  overflow: hidden;
}
.left-tree {
  width: 15%;
  height: 95%;
  background: #ffffff;
  overflow: auto;
  padding: 16px;
  border-right: 1px solid #dcdce6;
  border-radius: 4px 0;
  /deep/.el-input__inner {
    height: 32px;
    background: #ffffff;
    border-radius: 2px;
    border: 1px solid #e0e0e0;
  }
  .el-tree {
    padding-top: 16px;
    /deep/.el-tree-node {
      color: #333333;
      .el-tree-node__content {
        height: 30px;
      }
    }
    ::v-deep.el-tree-node.is-current > .el-tree-node__content {
      background-color: rgba(75, 113, 235, 0.16);
      border-left: 2px solid #4b71eb;
      color: #4b71eb;
    }
  }
}
.right-content {
  width: 85%;
}
.el-dialog-div {
  height: 60vh;
  overflow: hidden;
  // padding: 20px;
}
.server-main {
  position: relative;

  .search-form {
    background: #ffffff;
    padding: 16px 32px;
    border-top-right-radius: 4px;
    .el-form-item {
      margin-bottom: 0;
    }

    .form-input {
      margin-right: 32px;

      .el-input,
      .el-select {
        ::v-deep .el-input__inner {
          width: 240px;
          height: 32px;
          background: #ffffff;
          border-radius: 2px;
          border: 1px solid #e0e0e0;
        }
      }
    }
  }

  .server-table {
    margin-top: 0;
    padding: 16px 32px 64px;
    background: #ffffff;
    border-top: 1px solid #dcdce6;
    .little-circle-green {
      display: inline-block;
      width: 6px;
      height: 6px;
      border-radius: 3px;
      background: #55c898;
      position: relative;
      right: 4px;
      top: -2px;
    }

    .little-circle-red {
      display: inline-block;
      width: 6px;
      height: 6px;
      border-radius: 3px;
      background: #ee5564;
      position: relative;
      right: 6px;
      top: -2px;
    }

    .little-circle-yellow {
      display: inline-block;
      width: 6px;
      height: 6px;
      border-radius: 3px;
      background: #dba839;
      position: relative;
      right: 6px;
      top: -2px;
    }
    .table-title {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .table-title-text {
        margin: 8px 0;
        font-size: 16px;
        line-height: 16px;
        border-left: 5px solid $--color-primary;
        padding-left: 10px;
      }

      margin-bottom: 10px;
    }
  }

  .ontology-edit {
    padding: 0 10px;

    & + .ontology-edit {
      border-left: 1px solid #dddddd;
    }
  }

  ::v-deep .el-table__append-wrapper {
    padding: 10px;
  }

  ::v-deep .el-dialog {
    width: 500px;
    border-radius: 5px;
  }

  .dialog-inline {
    ::v-deep .el-form-item__content {
      width: 300px;
    }

    button.el-button {
      background-color: $--color-primary !important;
      color: #fff !important;
      line-height: 1.2;
    }
  }
}
</style>
